/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.course.subscribe;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import cn.com.chinasoa.course.subscribe.model.TrainingAddress;
import cn.com.chinasoa.course.subscribe.util.EntityManagerHelper;

import com.x4ee.soa.portal.framework.core.ActionConfig;
import com.x4ee.soa.portal.framework.core.ActionFilter;
import com.x4ee.soa.portal.framework.core.BaseRequestFilter;

/**
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-11-1上午02:09:34
 */
public class TrainingAddressCreateActionFilter extends BaseRequestFilter
		implements ActionFilter {

	private static String SUCCESS = "success";
	private static String FAILED = "error";

	public String process(ActionRequest actionRequest, ActionResponse actionResponse, ActionConfig actionConfig) 
		throws PortletException, IOException {
		
		String city = actionRequest.getParameter("city");
		String address = actionRequest.getParameter("address");
		String trafficInfo = actionRequest.getParameter("trafficInfo");
		String capacity = actionRequest.getParameter("capacity");
		
		boolean success = false;
		if((city!=null)&&(address!=null)&&(trafficInfo!=null)&&(capacity!=null)){
			int capacityInt= -1;
			try{capacityInt=Integer.parseInt(capacity);
			}catch(Exception e){}
			if(capacityInt>0){
				EntityManager em = EntityManagerHelper.getEntityManager();
				em.getTransaction().begin();
				TrainingAddress vTrainingAddress = new TrainingAddress();
				vTrainingAddress.setCity(city);
				vTrainingAddress.setAddress(address);
				vTrainingAddress.setTrafficInfo(trafficInfo);
				vTrainingAddress.setCapacity(capacityInt);				
				em.persist(vTrainingAddress);
				em.getTransaction().commit();
				EntityManagerHelper.closeEntityManager();
				success = true;
			}else{
				success = false;
			}
		}else{
			success = false;
		}
		if(success)return SUCCESS;
		else return FAILED;

	}

}
